package com.example.fujia.androidapp.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.example.fujia.androidapp.R;
import com.example.fujia.androidapp.tools.Tools;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by fujia on 2016/6/12.
 */
public class BlockActivity extends AppCompatActivity {
    private ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_block);
        this.listView = (ListView) findViewById(R.id.block_listView);

        Intent intent = getIntent();
        if (!Tools.objectIsNullOrNot(intent.getExtras().get("blockId"))){
            int blockId =  Integer.parseInt((String) intent.getExtras().get("blockId"));
            this.blockToService(blockId);
        }

    }

    private void blockToService(int blockId){
        RequestQueue queue = Volley.newRequestQueue(getApplicationContext());

        String url = "http://192.168.2.168:8080/json/article.action";

        Map<String, Integer> map = new HashMap<String, Integer>();
        map.put("blockId", blockId);
        JSONObject jsonObject = new JSONObject(map);

        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, jsonObject,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        Log.d("TAG", response.toString());
                        try {
                            JSONArray array = response.getJSONArray("articles");
                            List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
                            for (int i = 0;i < array.length();i++){
                                Map<String, Object> map = new HashMap<String, Object>();
                                JSONObject object = array.getJSONObject(i);
                                map.put("title", object.get("title"));
                                data.add(map);
                            }
                            Log.d("Article", data.toString());
                            SimpleAdapter adapter = new SimpleAdapter(getApplicationContext(), data, R.layout.item, new String[]{"title"}, new int[]{R.id.item_text});
                            listView.setAdapter(adapter);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("TAG", error.getMessage(), error);
            }
        });

        jsonObjectRequest.setRetryPolicy(new DefaultRetryPolicy(600000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

        queue.add(jsonObjectRequest);
    }

}
